/*
* @Author: Choisaaaa
* @Date: 2018/5/22  17:02 
*/

require("./seller-order-list.css");
var nav = require("page/common/nav/nav.js");
require("page/common/header/header.js");
var _mm = require("util/mm.js");
var navSide = require("page/common/nav-side/nav-side.js");              //侧边导航模块
var _order = require("service/seller/seller-order-service.js");
var Pagination = require("util/pagination/pagination.js");              //分页
var templateIndex = require("./seller-order-list.string");

var page = {
    data : {
        status : 0,
        productName : "",
        type : "list",
        pageNum : 1,
        pageSize : 10,
    },

    init : function(){
        this.onLoad();
        this.bindEvent();
    },
    bindEvent : function () {
        this.onClickSearch();//点击搜索按钮
        this.onClickPayed();//点击代发货按钮
    },
    onClickPayed : function () {
        var _this = this;
        $(document).on("click","#order-payed", function () {
            _order.sellerSearchOrder({
                pageNum : _this.data.pageNum,
                pageSize :_this.data.pageSize,
            }, function (res) {
                _this.data.pageNum = 1;//重置页数，防止数据冗余
                var html = _mm.renderHtml(templateIndex,res.data);
                $(".order-list").html(html);
            }, function (res) {
                $(".order-list").html("");
                _mm.errorTips(res.msg);
            })
        });
    },
    onClickSearch : function () {
        var _this = this;
        $(document).on("click","#search-order", function () {
            var orderNo = $("#orderNo").val();
            if(!orderNo){
                _mm.errorTips("请输入订单号")
                return;
            }
            var reg = new RegExp("^[0-9]*$");
            if (!reg.test($.trim(orderNo))) {
                _mm.errorTips("输入的订单号格式不正确，订单号只由数字组成")
                return ;
            }
            _order.sellerSearchOrder(orderNo, function (res) {
                var html = _mm.renderHtml(templateIndex,res.data);
                $(".order-list").html(html);
            }, function (res) {
                $(".order-list").html("");
                _mm.errorTips(res.msg);
            })
        });
    },
    onLoad : function () {
        this.onLoadSide();              //加载侧边导航
        this.onLoadOrderList(); //加载订单列表
    },
    onLoadOrderList : function () {
        var _this = this;
        _order.getSellerOrderList({
            pageNum : _this.data.pageNum,
            pageSize : _this.data.pageSize
        }, function (res) {
            _this.data.pageNum = 1;//重置页数，防止数据冗余
            var html = _mm.renderHtml(templateIndex,res.data);
            $(".order-list").html(html);
            _this.loadPagination({//加载分页信息
                hasPreviousPage : res.data.hasPreviousPage,//是否有上一页，boolean
                prePage         : res.data.prePage,//上一页的编号
                hasNextPage     : res.data.hasNextPage,//是否有上一页，boolean
                nextPage        : res.data.nextPage,//下一页的编号
                pageNum         : res.data.pageNum,//当前的页号
                pages           : res.data.pages//总的页数
            });
        }, function (err) {
            
        })
    },

    /** 1.初始化加载页面时的数据渲染 **/
    onLoadSide : function(){ //1.1:初始化侧边导航
        navSide.init({
            name : "seller-center"
        });
    },



    //3.辅助函数
    loadPagination : function(pageInfo){//3.1分页
        var _this = this;
        this.pagination ? "" : (this.pagination = new Pagination());
        this.pagination.render($.extend({},pageInfo,{
            container : $(".pagination"),
            onSelectPage : function(pageNum){
                _this.data.pageNum = pageNum; //保存从分页组件中点击的页数
                _this.onLoadOrderList();
            }
        }));
    },
}
$(function () {
    page.init();
});